home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / event.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  10.6 KB  |  478 lines

  1. /*
  2.  * @(#)Event.java    1.28 95/10/26 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.io.*;
  22.  
  23. /**
  24.  * Event is a platform-independent class that encapsulates events from
  25.  * the local Graphical User Interface(GUI) platform.
  26.  *
  27.  * @version 1.28 10/26/95
  28.  * @author Sami Shaio
  29.  */
  30. public class Event {
  31.     /* Modifier constants */
  32.  
  33.     /**
  34.      * The shift modifier constant.
  35.      */
  36.     public static final int SHIFT_MASK         = 1 << 0;
  37.  
  38.     /**
  39.      * The control modifier constant.
  40.      */
  41.     public static final int CTRL_MASK         = 1 << 1;
  42.  
  43.     /** 
  44.      * The meta modifier constant.
  45.      */
  46.     public static final int META_MASK         = 1 << 2;
  47.  
  48.     /** 
  49.      * The alt modifier constant.
  50.      */
  51.     public static final int ALT_MASK         = 1 << 3;
  52.  
  53.     /* Action keys */
  54.     /** 
  55.      * The home key.
  56.      */
  57.     public static final int HOME         = 0;
  58.  
  59.     /** 
  60.      * The end key. 
  61.      */
  62.     public static final int END         = 1;
  63.  
  64.     /**
  65.      * The page up key.
  66.      */
  67.     public static final int PGUP         = 2;
  68.  
  69.     /**
  70.      * The page down key.
  71.      */
  72.     public static final int PGDN         = 3;
  73.  
  74.     /**
  75.      * The up arrow key.
  76.      */
  77.     public static final int UP             = 4;
  78.  
  79.     /**
  80.      * The down arrow key.
  81.      */
  82.     public static final int DOWN         = 5;
  83.  
  84.     /**
  85.      * The left arrow key.
  86.      */
  87.     public static final int LEFT         = 6;
  88.  
  89.     /**
  90.      * The right arrow key.
  91.      */
  92.     public static final int RIGHT         = 7;
  93.  
  94.     /**
  95.      * The F1 function key
  96.      */
  97.     public static final int F1            = 8;
  98.  
  99.     /**
  100.      * The F2 function key
  101.      */
  102.     public static final int F2            = 9;
  103.  
  104.     /**
  105.      * The F3 function key
  106.      */
  107.     public static final int F3            = 10;
  108.  
  109.     /**
  110.      * The F4 function key
  111.      */
  112.     public static final int F4            = 11;
  113.  
  114.     /**
  115.      * The F5 function key
  116.      */
  117.     public static final int F5            = 12;
  118.  
  119.     /**
  120.      * The F6 function key
  121.      */
  122.     public static final int F6            = 13;
  123.  
  124.     /**
  125.      * The F7 function key
  126.      */
  127.     public static final int F7            = 14;
  128.  
  129.     /**
  130.      * The F8 function key
  131.      */
  132.     public static final int F8            = 15;
  133.  
  134.     /**
  135.      * The F9 function key
  136.      */
  137.     public static final int F9            = 16;
  138.  
  139.     /**
  140.      * The F10 function key
  141.      */
  142.     public static final int F10            = 17;
  143.  
  144.     /**
  145.      * The F11 function key
  146.      */
  147.     public static final int F11            = 18;
  148.  
  149.     /**
  150.      * The F12 function key
  151.      */
  152.     public static final int F12            = 19;
  153.  
  154.  
  155.     /* Base for all window events. */
  156.     private static final int WINDOW_EVENT     = 200;
  157.  
  158.     /**
  159.      * The destroy window event.
  160.      */
  161.     public static final int WINDOW_DESTROY     = 1 + WINDOW_EVENT;
  162.  
  163.     /**
  164.      * The expose window event. 
  165.      */
  166.     public static final int WINDOW_EXPOSE     = 2 + WINDOW_EVENT;
  167.  
  168.     /** 
  169.      * The iconify window event. 
  170.      */
  171.     public static final int WINDOW_ICONIFY    = 3 + WINDOW_EVENT;
  172.  
  173.     /** 
  174.      * The de-iconify window event.
  175.      */
  176.     public static final int WINDOW_DEICONIFY    = 4 + WINDOW_EVENT;
  177.  
  178.     /**
  179.      * The move window event.
  180.      */
  181.     public static final int WINDOW_MOVED    = 5 + WINDOW_EVENT;
  182.  
  183.     /* Base for all keyboard events. */
  184.     private static final int KEY_EVENT         = 400;
  185.  
  186.     /**
  187.      * The key press keyboard event.
  188.      */
  189.     public static final int KEY_PRESS         = 1 + KEY_EVENT;
  190.  
  191.     /**
  192.      * The key release keyboard event.
  193.      */
  194.     public static final int KEY_RELEASE     = 2 + KEY_EVENT;
  195.  
  196.     /** 
  197.      * The key action keyboard event. 
  198.      */
  199.     public static final int KEY_ACTION         = 3 + KEY_EVENT;
  200.  
  201.     /** 
  202.      * The key action keyboard event. 
  203.      */
  204.     public static final int KEY_ACTION_RELEASE    = 4 + KEY_EVENT;
  205.  
  206.     /* Base for all mouse events. */
  207.     private static final int MOUSE_EVENT     = 500;
  208.  
  209.     /**
  210.      * The mouse down event.
  211.      */
  212.     public static final int MOUSE_DOWN         = 1 + MOUSE_EVENT;
  213.  
  214.     /**
  215.      *  The mouse up event.
  216.      */
  217.     public static final int MOUSE_UP         = 2 + MOUSE_EVENT;
  218.  
  219.     /**
  220.      * The mouse move event.
  221.      */
  222.     public static final int MOUSE_MOVE         = 3 + MOUSE_EVENT;
  223.  
  224.     /**
  225.      * The mouse enter event.
  226.      */
  227.     public static final int MOUSE_ENTER     = 4 + MOUSE_EVENT;
  228.  
  229.     /**
  230.      * The mouse exit event.
  231.      */
  232.     public static final int MOUSE_EXIT         = 5 + MOUSE_EVENT;
  233.  
  234.     /** 
  235.      * The mouse drag event.
  236.      */
  237.     public static final int MOUSE_DRAG         = 6 + MOUSE_EVENT;
  238.  
  239.  
  240.     /* Scrolling events */
  241.     private static final int SCROLL_EVENT     = 600;
  242.  
  243.     /** 
  244.      * The line up scroll event. 
  245.      */
  246.     public static final int SCROLL_LINE_UP    = 1 + SCROLL_EVENT;
  247.  
  248.     /**
  249.      * The line down scroll event.
  250.      */
  251.     public static final int SCROLL_LINE_DOWN    = 2 + SCROLL_EVENT;
  252.  
  253.     /**
  254.      * The page up scroll event.
  255.      */
  256.     public static final int SCROLL_PAGE_UP    = 3 + SCROLL_EVENT;
  257.  
  258.     /**
  259.      * The page down scroll event.
  260.      */
  261.     public static final int SCROLL_PAGE_DOWN    = 4 + SCROLL_EVENT;
  262.  
  263.     /**
  264.      * The absolute scroll event.
  265.      */
  266.     public static final int SCROLL_ABSOLUTE    = 5 + SCROLL_EVENT;
  267.     
  268.     /* List Events */
  269.     private static final int LIST_EVENT        = 700;
  270.  
  271.     /* Event sent when an item has been selected */
  272.     public static final int LIST_SELECT        = 1 + LIST_EVENT;
  273.  
  274.     /* Event sent when an item has been deselected */
  275.     public static final int LIST_DESELECT    = 2 + LIST_EVENT;
  276.  
  277.     /* Misc Event */
  278.     private static final int MISC_EVENT        = 1000;
  279.  
  280.     /**
  281.      * An action event.
  282.      */
  283.     public static final int ACTION_EVENT    = 1 + MISC_EVENT;
  284.  
  285.     /**
  286.      * A file loading event.
  287.      */
  288.     public static final int LOAD_FILE        = 2 + MISC_EVENT;
  289.  
  290.     /**
  291.      * A file saving event.
  292.      */
  293.     public static final int SAVE_FILE        = 3 + MISC_EVENT;
  294.  
  295.     /**
  296.      * A component gained the focus.
  297.      */
  298.     public static final int GOT_FOCUS        = 4 + MISC_EVENT;
  299.  
  300.     /**
  301.      * A component lost the focus.
  302.      */
  303.     public static final int LOST_FOCUS        = 5 + MISC_EVENT;
  304.     
  305.     /**
  306.      * The target component.
  307.      */
  308.     public Object target;
  309.  
  310.     /**
  311.      * The time stamp.
  312.      */
  313.     public long when;
  314.  
  315.     /**
  316.      * The type of this event. 
  317.      */
  318.     public int id;
  319.  
  320.     /** 
  321.      * The x coordinate of the event.
  322.      */
  323.     public int x;
  324.  
  325.     /** 
  326.      * The y coordinate of the event. 
  327.      */
  328.     public int y;
  329.  
  330.     /** 
  331.      * The key that was pressed in a keyboard event. 
  332.      */
  333.     public int key;
  334.  
  335.     /** 
  336.      * The state of the modifier keys.
  337.      */
  338.     public int modifiers;
  339.  
  340.     /**
  341.      * An arbitraty argument.
  342.      */
  343.     public Object arg;
  344.  
  345.     /**
  346.      * The next event. Used when putting events into a linked list.
  347.      */
  348.     public Event evt;
  349.  
  350.     /**
  351.      * Constructs an event with the specified target component, time stamp,
  352.      * event type, x and y coordinates, keyboard key, state of the modifier
  353.      * keys and argument.
  354.      * @param target the target component
  355.      * @param when the time stamp
  356.      * @param id the event type
  357.      * @param x the x coordinate
  358.      * @param y the y coordinate
  359.      * @param key the key pressed in a keyboard event
  360.      * @param modifiers the state of the modifier keys
  361.      * @param arg the specified argument
  362.      */
  363.     public Event(Object target, long when, int id, int x, int y, int key,
  364.          int modifiers, Object arg) {
  365.     this.target = target;
  366.     this.when = when;
  367.     this.id = id;
  368.     this.x = x;
  369.     this.y = y;
  370.     this.key = key;
  371.     this.modifiers = modifiers;
  372.     this.arg = arg;
  373.     }
  374.  
  375.     /**
  376.      * Constructs an event with the specified target component, time stamp,
  377.      * event type, x and y coordinates, keyboard key, state of the modifier
  378.      * keys and an argument set to null. 
  379.      * @param target the target component
  380.      * @param when the time stamp
  381.      * @param id the event type
  382.      * @param x the x coordinate
  383.      * @param y the y coordinate
  384.      * @param key the key pressed in a keyboard event
  385.      * @param modifiers the state of the modifier keys
  386.      */
  387.     public Event(Object target, long when, int id, int x, int y, int key, int modifiers) {
  388.     this(target, when, id, x, y, key, modifiers, null);
  389.     }
  390.  
  391.     /**
  392.      * Constructs an event with the specified target component, 
  393.      * event type, and argument. 
  394.      * @param target the target component
  395.      * @param id the event type
  396.      * @param arg the specified argument
  397.      */
  398.     public Event(Object target, int id, Object arg) {
  399.     this(target, 0, id, 0, 0, 0, 0, arg);
  400.     }
  401.  
  402.     /** 
  403.      * Translates an event relative to the given component. This
  404.      * involves at a minimum translating the coordinates so they make
  405.      * sense within the given component. It may also involve
  406.      * translating a region in the case of an expose event.
  407.      * @param x the x coordinate
  408.      * @param y the y coordinate
  409.      */
  410.     public void translate(int x, int y) {
  411.     this.x += x;
  412.     this.y += y;
  413.     }
  414.  
  415.     /**
  416.      * Checks if the shift key is down.
  417.      * @see #modifiers
  418.      * @see #controlDown
  419.      * @see #metaDown
  420.      */
  421.     public boolean shiftDown() {
  422.     return (modifiers & SHIFT_MASK) != 0;
  423.     }
  424.  
  425.     /**
  426.      * Checks if the control key is down.
  427.      * @see #modifiers
  428.      * @see #shiftDown
  429.      * @see #metaDown
  430.      */
  431.     public boolean controlDown() {
  432.     return (modifiers & CTRL_MASK) != 0;
  433.     }
  434.  
  435.     /**
  436.      * Checks if the meta key is down.
  437.      * @see #modifiers
  438.      * @see #shiftDown
  439.      * @see #controlDown
  440.      */
  441.     public boolean metaDown() {
  442.     return (modifiers & META_MASK) != 0;
  443.     }
  444.  
  445.     /**
  446.      * Returns the parameter String of this Event. 
  447.      */
  448.     protected String paramString() {
  449.     String str = "id=" + id + ",x=" + x + ",y=" + y;
  450.     if (key != 0) {
  451.         str += ",key=" + key;
  452.     }
  453.     if (shiftDown()) {
  454.         str += ",shift";
  455.     }
  456.     if (controlDown()) {
  457.         str += ",control";
  458.     }
  459.     if (metaDown()) {
  460.         str += ",meta";
  461.     }
  462.     if (target != null) {
  463.         str += ",target=" + target;
  464.     }
  465.     if (arg != null) {
  466.         str += ",arg=" + arg;
  467.     }
  468.     return str;
  469.     }
  470.  
  471.     /**
  472.      * Returns the String representation of this Event's values.
  473.      */
  474.     public String toString() {
  475.     return getClass().getName() + "[" + paramString() + "]";
  476.     }
  477. }
  478.